package com.central.apps.controller;


import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/excel")
@Slf4j
public class GetExcelInfos {

    @RequestMapping("/getExcelInfos")
    public Map<String, Object> getExcelInfos(MultipartFile file) {
        Map<String, Object> map = new HashMap<>();
        InputStream input = null;
        String fileName =file.getOriginalFilename();
        fileName =fileName.substring(fileName.length()-3);
        if("xls".equals(fileName)){
            HSSFWorkbook wb = null;
            try {
                input = file.getInputStream();
                // 创建文档
                wb = new HSSFWorkbook(input);
                // 读取sheet(页)
                for (int numSheet = 0; numSheet < wb.getNumberOfSheets(); numSheet++) {
                    HSSFSheet hssfSheet = wb.getSheetAt(numSheet);
                    if (hssfSheet != null) {
                        List<ArrayList<String>> list = new ArrayList<>();
                        ArrayList<String> rowList = null;
                        if (hssfSheet != null) {
                            int totalRows = hssfSheet.getLastRowNum();
                            // 读取Row
                            for (int rowNum = 1; rowNum <= totalRows; rowNum++) {
                                HSSFRow hssfRow = hssfSheet.getRow(rowNum);
                                if (hssfRow != null) {
                                    rowList = new ArrayList<>();
                                    short totalCells = hssfRow.getLastCellNum();
                                    // 读取列，从第一列开始
                                    for (short c = 0; c <= totalCells + 1; c++) {
                                        HSSFCell cell = hssfRow.getCell(c);
                                        if (cell == null) {
                                            rowList.add(ExcelUtil.EMPTY);
                                            continue;
                                        }
                                        rowList.add(ExcelUtil.getHValue(cell).trim());
                                    }
                                    list.add(rowList);
                                }
                            }
                        }
                        map.put(hssfSheet.getSheetName(), list);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }else{
            log.info("进入XSSFWorkbook+++++++++++++++++++++++++++++++++++++++++++++++");
            XSSFWorkbook wb = null;
            try {
                input = file.getInputStream();
                // 创建文档
                wb = new XSSFWorkbook(input);
                // 读取sheet(页)
                for (int numSheet = 0; numSheet < wb.getNumberOfSheets(); numSheet++) {
                    XSSFSheet xssfSheet = wb.getSheetAt(numSheet);
                    if (xssfSheet != null) {
                        List<ArrayList<String>> list = new ArrayList<>();
                        ArrayList<String> rowList = null;
                        if (xssfSheet != null) {
                            int totalRows = xssfSheet.getLastRowNum();
                            log.info("totalRows+++++++++++++++++++++++++++++++"+totalRows);
                            // 读取Row
                            for (int rowNum = 1; rowNum <= totalRows; rowNum++) {
                                XSSFRow xssfRow = xssfSheet.getRow(rowNum);
                                if (xssfRow != null) {
                                    rowList = new ArrayList<>();
                                    short totalCells = xssfRow.getLastCellNum();
                                    // 读取列，从第一列开始
                                    for (short c = 0; c <= totalCells + 1; c++) {
                                        XSSFCell cell = xssfRow.getCell(c);
                                        if (cell == null) {
                                            rowList.add(ExcelUtil.EMPTY);
                                            continue;
                                        }
                                        rowList.add(ExcelUtil.getXValue(cell).trim());
                                    }
                                    list.add(rowList);
                                }
                            }
                        }
                        map.put(xssfSheet.getSheetName(), list);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        map.put("code",200);
        map.put("message","succeed");
        return map;
    }
}
